import socket, paramiko, time, threading

host = socket.gethostbyname(socket.gethostname())
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(str("localhost"),int(22), 'kangta123', 'hyxkangta123@')
chan = ssh.get_transport().open_session()
#chan.setblocking(0)
chan.get_pty()
chan.invoke_shell()

#_lock = threading.Lock()
def recv():
    num = 0
    while True:
        print "before recv"
        res = chan.recv(1024)
        print "after recv"
        if chan.exit_status_ready():
            break
        print str(num) + ":" +res
        num += 1

t = threading.Thread(target=recv)
t.daemon = True
t.start()

#time.sleep(10)
cmds = ["cd /tmp", "ls -lh"]
def sender():
    index = 0
    while True:
        print "send :" + cmds[index]
        chan.send(cmds[index]+"\n")
        print "after send"
        time.sleep(1)
        #chan.shutdown(2)
        index += 1
        if index >= len(cmds):
            break

t1 = threading.Thread(target=sender)
t1.daemon = True
t1.start()

time.sleep(20)
